package com.common.springbootcommonmessage.service.impl;

import com.common.springbootcommonmessage.constant.Constant;
import com.common.springbootcommonmessage.service.CommonOrderService;
import com.common.springbootcommonmessage.utils.CommonRedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author korol *
 * @date 2022年12月09日 15:26
 */
@Service
public class CommonOrderServiceImpl implements CommonOrderService {

    @Autowired
    private CommonRedisUtil redisUtil;

    @Override
    public Integer getMaxGroupId(String site, Integer groupId) {
        try {
            String messageGroupIdKey = Constant.MESSAGE_GROUP_ID_PREFIX + site + groupId;

            while (this.redisUtil.hasKey(messageGroupIdKey)) {
                groupId = groupId + 1;
                messageGroupIdKey = Constant.MESSAGE_GROUP_ID_PREFIX + site + groupId;
            }

            this.redisUtil.set(messageGroupIdKey, groupId, 600);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return groupId;
    }

    @Override
    public void deleteMaxGroupIdKey(String site, Integer groupId) {

        try {
            String messageGroupIdKey = Constant.MESSAGE_GROUP_ID_PREFIX + site + groupId;
            if (this.redisUtil.hasKey(messageGroupIdKey)) this.redisUtil.del(messageGroupIdKey);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
